package com.project.controller;

import com.project.bean.ShoppingCartBean;
import com.project.bean.UserBean;
import com.project.service.IShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {
    @Autowired
    private IShoppingCartService shopService;
    @RequestMapping("buy")
    public String buy(Integer productId, HttpServletRequest request){
        //从session中取出登录用户
        UserBean user = (UserBean) request.getSession().getAttribute("loginUser");
        //完成购物
        shopService.buy(productId,user.getUserId());

        return "ok";
    }

    @RequestMapping("remove")
    public String remove(Integer shopId, HttpServletRequest request){
        shopService.remove(shopId);
        return "ok";
    }

    @RequestMapping("updateNum")
    public String updateNum(Integer shopId, Integer num,HttpServletRequest request){
        shopService.updateNum(shopId,num);
        return "ok";
    }

    @RequestMapping("showShop")
    public List<ShoppingCartBean> showShop(HttpServletRequest request){
        UserBean user = (UserBean) request.getSession().getAttribute("loginUser");
        return shopService.findByUserId(user.getUserId());
    }


}
